/* 简单工厂(simple factory),创建一个工厂类,通过传入的参数创建相应的类 */

#include <iostream>
#include <memory>

using namespace std;

enum EnumAlg {
	EnumAlgThreshold = 0,
	EnumAlgGrowing
};

// 算法抽象接口类
class Segment 
{
public:
	virtual void print_result() = 0;
};

#pragma region Threshold

class SegmentThreshold : public Segment
{
public:
	void print_result();
};

void SegmentThreshold::print_result()
{
	cout << "Using threshold to segment." << endl;
}

#pragma endregion

#pragma region Growing

class SegmentGrowing : public Segment
{
public:
	void print_result();
};

void SegmentGrowing::print_result()
{
	cout << "Using growing to segment." << endl;
}

#pragma endregion

#pragma region Factory

class Factory
{
public:
	Segment* set_alg(EnumAlg type);

private:

};

Segment* Factory::set_alg(EnumAlg type)
{
	switch (type) {
	case EnumAlgThreshold:
		return new SegmentThreshold();
	case EnumAlgGrowing:
		return new SegmentGrowing();
	default:
		return nullptr;
	}
}

#pragma endregion


int main(int argc, char const *argv[])
{
	Factory fractory;
	unique_ptr<Segment> ptr = nullptr;

	// 实际应用中，只需要修改这行代码就可以切换不同的算法，
	// 前后的代码都不需要修改，减少了代码的修改量。
	ptr.reset(fractory.set_alg(EnumAlgThreshold));
	ptr->print_result();

	cout<<"\n\nChange alg...."<<endl;
	ptr.reset(fractory.set_alg(EnumAlgGrowing));
	ptr->print_result();

	return 0;
}

